﻿@using Lib.core;
@using Hiwjcn.Web.Models.Category;
@{
    Layout = null;
    var model = ViewData["model"] as CategoryManageViewModel;
    if (model == null) { model = new CategoryManageViewModel(); }
}

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
          "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <!-- header page -->
    @Html.Partial(IncludePath("header"))

    <link href="~/ui/jq-plus/ztree/css/zTreeStyle/zTreeStyle.css" rel="stylesheet" />
    <script src="~/ui/jq-plus/ztree/js/jquery.ztree.all-3.5.min.js"></script>
</head>

<body>
    @Html.Partial(IncludePath("body_top"))
    @Html.Partial(IncludePath("manage_nav"))

    <script src="~/ui/jq-plus/layer/layer.js"></script>

    <div class="container">

        <div class="row">
            <div class="col-md-6">
                <div class="box">
                    <form action="/admin/category/categorymanage/" method="get">
                        <div class="input-group">
                            <span class="input-group-addon">选择树</span>
                            <select class="form-control" name="type" data-value="@(model.CategoryType)">
                                @if (ValidateHelper.IsPlumpList(model.TypesList))
                                {
                                    foreach (var t in model.TypesList)
                                    {
                                        <option value="@(t)">@(t)</option>
                                    }
                                }
                            </select>
                            <span class="input-group-btn">
                                <button type="submit" class="btn btn-default">Go</button>
                            </span>
                        </div>
                    </form>
                </div>
                <div class="box">
                    <div style="overflow-x:auto;margin:20px 0;">
                        <h4>树结构：</h4>
                        <ul id="treeDemo" class="ztree"></ul>
                    </div>
                </div>

            </div>
            <div class="col-md-6">

                <div class="box">
                    @if (ValidateHelper.IsPlumpList(model.ErrList))
                    {
                        <div class="alert alert-danger">树结构存在错误节点，建议清理</div>
                        <h4>错误节点：</h4>
                        <table class="table table-hover">
                            <thead>
                                <tr>
                                    <td>名称</td>
                                    <td>层级</td>
                                    <td>描述</td>
                                </tr>
                            </thead>
                            <tbody>
                                @foreach (var err in model.ErrList)
                                {
                                    <tr>
                                        <td>@(err.CategoryName)</td>
                                        <td>@(err.CategoryLevel + 1)</td>
                                        <td>@(err.CategoryDescription)</td>
                                    </tr>
                                }
                            </tbody>
                        </table>
                        <p>
                            <button class="btn btn-primary" id="del_err">删除错误节点</button>
                        </p>
                    }
                    else
                    {
                        <div class="alert alert-info">数结构没有错误，出现错误节点将建议清理</div>
                    }
                    <div class="alert alert-success">
                        <h4><span class="fa fa-info"></span>提示：</h4>
                        <p>1.存在错误节点的树结构将不显示！</p>
                        <p>2.任何清理操作都有可能造成数据丢失，请谨慎操作！</p>
                    </div>
                </div>

            </div>
        </div>

        <script type="text/javascript">
            var category_type = '@(model.CategoryType)';
        </script>

        @if (model.HasNodes && !ValidateHelper.IsPlumpList(model.ErrList))
        {
            <script type="text/javascript">
                $(function () {
                    //树设置
                    var setting = {
                        data: {
                            simpleData: {
                                enable: true
                            }
                        },
                        callback: {
                            onClick: function onClick(event, treeId, treeNode, clickFlag) {
                                var id = treeNode["id"];
                                layer.open({
                                    type: 2,
                                    title: '编辑节点',
                                    shadeClose: true,
                                    shade: 0.8,
                                    area: ['700px', '90%'],
                                    content: '/Admin/Category/EditNode/?node_id=' + id
                                });
                            }
                        }
                    };

                    //树的数据
                    var data = [@(Html.Raw(model.Json))][0];

                    //树对象
                    var tree = $.fn.zTree.init($("#treeDemo"), setting, data);
                    tree.expandAll(true);
                });
            </script>
        }

        <script type="text/javascript">
            $(function () {
                //清理错误节点
                $('#del_err').click(function () {
                    postJson('/admin/category/ClearTree/', {
                        category_type: category_type
                    }, function (json) {
                        if (json.success) {
                            alert('处理成功');
                            window.location.reload();
                        }
                        else {
                            alert(json.msg);
                        }
                    });
                });
            });
        </script>

    </div>
    <!-- footer page -->
    @Html.Partial(IncludePath("body_bottom"))
</body>
</html>
